<template>
  <div class="app-container">
    <bread parentPathText="地区管理" currentPathText="社区管理" />
    <el-table v-loading="loading" :data="tableList">
      <el-table-column
        align="center"
        label="网格"
        prop="name"
        min-width="20%"
      />
      <el-table-column
        align="center"
        label="微网格数"
        prop="miniGridCount"
        :show-overflow-tooltip="true"
        min-width="20%"
      />
      <el-table-column
        align="center"
        label="居民户数"
        prop="houseCount"
        min-width="20%"
      />
      <el-table-column
        align="center"
        label="居民人数"
        prop="userCount"
        min-width="30%"
      />
      <el-table-column
        align="center"
        label="商户"
        prop="businessNum"
        min-width="15%"
      />
      <el-table-column
        align="center"
        label="单位"
        prop="unitNum"
        min-width="15%"
      />
      <el-table-column
        align="center"
        label="操作"
        min-width="25%"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑弹框 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisible"
      width="22%"
      :before-close="handleClose"
      append-to-body
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
      <el-form-item label="微网格数：" prop="miniGridNum">
          <el-input-number
            v-model="ruleForm.miniGridNum"
            type="number"
            :min="1"
          />
        </el-form-item>
        <el-form-item label="居民户数：" prop="houseNum">
          <el-input-number
            v-model="ruleForm.houseNum"
            type="number"
            :min="1"
          />
        </el-form-item>
        <el-form-item label="居民人数：" prop="peopleNum">
          <el-input-number
            v-model="ruleForm.peopleNum"
            type="number"
            :min="1"
          />
        </el-form-item>
        <el-form-item label="商户数量：" prop="businessNum">
          <el-input-number
            v-model="ruleForm.businessNum"
            type="number"
            :min="1"
          />
        </el-form-item>
        <el-form-item label="单位数量：" prop="unitNum">
          <el-input-number
            v-model="ruleForm.unitNum"
            :min="1"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('ruleForm')" style="padding:12px 100px;"
          >确定</el-button
        >
      </div>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="pagination"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryParams: {
        gridVal: "",
        groupVal: "",
        type: "",
        info: "",
      },
      id:'',
      pageNum: 1,
      pageSize: 10,
      loading: false,
      total: 0,
      ruleForm: {
        businessNum: "",
        unitNum: "",
        miniGridNum:'',
        peopleNum:'',
        houseNum:'',
      },
      tableList: [],
      dialogVisible: false,
      time: new Date().getTime(),
      rules: {
        businessNum: [
          { required: true, message: "请输入商户数量", trigger: "blur" },
        ],
        unitNum: [
          { required: true, message: "请输入单位数量", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      let params = {
        time: this.time,
      };
      let result = this.$md5.transferValue(params);
      this.axios
        .post(this.stateUrl.dataModel.gridList, {
          time: this.time,
          sign: this.$md5.val(result),
        })
        .then((res) => {
          this.tableList = res.data.data;
        })
        .catch(() => {});
    },
    // 编辑
    handleUpdate(row) {
      this.dialogVisible = true;
      this.id = row.id;
      let params = {
        time: this.time,
        id: row.id,
      };
      let result = this.$md5.transferValue(params);
      this.axios
        .post(this.stateUrl.region.detail, {
          time: this.time,
          id: row.id,
          sign: this.$md5.val(result),
        })
        .then((res) => {
          this.ruleForm.businessNum = res.data.data.businessNum;
          this.ruleForm.unitNum = res.data.data.unitNum;
          this.ruleForm.miniGridNum = res.data.data.miniGridCount;
          this.ruleForm.peopleNum = res.data.data.userCount;
          this.ruleForm.houseNum = res.data.data.houseCount;
        })
        .catch(() => {});
    },
    // 关闭弹框
    handleClose() {
      this.$refs["ruleForm"].resetFields();
      this.dialogVisible = false;
    },
     // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
           let params = {
             parentId:0,
             id: this.id,
             time:this.time,
             businessNum:this.ruleForm.businessNum,
             unitNum:this.ruleForm.unitNum,
             miniGridNum:this.ruleForm.miniGridNum,
             peopleNum:this.ruleForm.peopleNum,
             houseNum:this.ruleForm.houseNum,
           }
           let result = this.$md5.transferValue(params);
           this.axios.post(this.stateUrl.region.edit,{
             parentId:0,
             id: this.id,
             time:this.time,
             businessNum:this.ruleForm.businessNum,
             unitNum:this.ruleForm.unitNum,
             miniGridNum:this.ruleForm.miniGridNum,
             peopleNum:this.ruleForm.peopleNum,
             houseNum:this.ruleForm.houseNum,
             sign: this.$md5.val(result),
           }).then(res=>{
             this.$message.success("操作成功");
             this.$refs[formName].resetFields();
              this.dialogVisible = false;
              this.getList();
           }).catch(()=>{

           })
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
    },
  },
};
</script>
<style lang="less" scoped>
.dialog-footer{
  text-align: center;
}
/deep/ .el-dialog{
  min-width:326px !important;
}
/deep/ .el-input-number {
 min-width: 90%;
}
</style>